<?php
/**
 * Created by PhpStorm
 * User: shen.zk
 * Date: 2023/9/6
 * Time: 13:58
 * Comment: ''
 */

namespace App\Http\Controllers\Common;

use App\Http\Controllers\BaseController;
use App\Service\Common\CheckSignService;
use Illuminate\Support\Facades\Cache;

class TestController extends BaseController
{

    protected $servicePrivateKey = 'D:\phpStudy\WWW\openapi\keys\self\private.pem';

    protected $servicePublicKey = 'D:\phpStudy\WWW\openapi\keys\self\public.pem';

    protected $shandePrivateKey = 'D:\phpStudy\WWW\openapi\keys\coupon\private.pem';

    protected $shandePubKey = 'D:\phpStudy\WWW\openapi\keys\coupon\public.pem';

    public function testSign()
    {
        // 登入
        $request = [
            'phone'=>'13651742952',
            'access_mid'=>'123456',
            'request_time'=>time(),
        ];

        $sign = CheckSignService::getInstance()->creteSign($request,$this->privateKey);
        $data['data'] = $request;
        $data['sign'] = $sign;
        return response()->json($data);
    }

    public function checkSign()
    {
        $request = request()->all();
        var_dump($request);
        $sign = CheckSignService::getInstance()->checkRequestSign($request['data'],$request['sign'],$this->publicKey);
        var_dump($sign);
    }

    public function testShandeCreateSign()
    {

        $data = request()->all();
        $res = CheckSignService::getInstance()->selfRequestCreateSign($data,file_get_contents($this->servicePublicKey),file_get_contents($this->shandePrivateKey));
        $res['access_mid'] = '123456';
        $res['timestamp'] = 1694085373;
        echo json_encode($res);
    }

    public function testShandeVerifySign()
    {
        $request = request()->all();
        CheckSignService::getInstance()->verifySign($request['data'],$request['sign'],file_get_contents($this->shandePubKey));

        $aeskey = CheckSignService::getInstance()->decryptByPri($request['encryptKey'],file_get_contents($this->servicePrivateKey));

        $response = CheckSignService::getInstance()->aesDecrypt($request['data'],$aeskey);
        echo '打印请求参数';
        print_r(json_decode($response,true));

    }

    public function ShandeVerifySign()
    {
        $request = request()->all();
        CheckSignService::getInstance()->verifySign($request['data'],$request['sign'],file_get_contents($this->servicePublicKey));

        $aeskey = CheckSignService::getInstance()->decryptByPri($request['encryptKey'],file_get_contents($this->shandePrivateKey));

        $response = CheckSignService::getInstance()->aesDecrypt($request['data'],$aeskey);
        echo '打印请求参数';
        print_r(json_decode($response,true));

    }

    public function clearKey()
    {
        $key = request('keys');
        if (Cache::has($key)) {
            echo "key存在";
            var_dump(Cache::forget($key));
        } else {
            echo "key不存在";
        }
    }

    public function queueWork()
    {

        // 异步记录请求日志
        $log = [
            'action'      => 'https://www.baidu.com',
            'apply_info'  => '{"data":"n\/WWPlaVAmhMSbFUzA6n2brkkXjXP4kGWi+HvG3o\/WlkNVlmua2ALr+orvdqlpcNOm\/SzXDLNcLyXtwvc9sAN\/9rxqxXGysLwzg\/FwB\/MN8=","encryptKey":"lCy7f9vCXE\/hvrxXSJ4bqX2A1eRwB9rI5Gyi059rFf7LmqsUSnjOmxtIiSs7dlvQnAzRxdvxqB5fHYygZeHKKQhkgcccLh1MCwR9TZ+WDqZZfMCqQBQl\/r2viBAZ9kHcZRPgCSV8+xd24yeBcAW2V8cyHYMeuLOZ1FCiL2PU+8ZtouXHDF9jPcZ+tAFrTNqMVZbwJXD4CIwLisWvYlSofpPsDhaFP5hwrwKqWKufmaGzeDFstA94BQbho5JxdphBTtwmeqA1XTQY75n8kFkPtmtBCV5saAz6rNN+Eq9bhFpMNtO+Xp+RthnS3IzZsEtNO8SUa4TeMN477L1kgd7qjw==","sign":"S5+lkC7qa4dFhEr59J4fDt0wXeXYQagLjypZHYwMxhkBKrIvODTv118jeqMgcmq6m6ZftNTIIz2JtTaWVojtKiw8l6x2qvQj7toKqCIeqYwsjwRBomucZhwRUNYNQ5QJEmEeWJnhNjV9zs6KiNjtm1fo2aLJIsAWbEJsjurLgtw0VoapUDyU5qoSGbz2J2eU0gPUyNLFLyQmWDspsj6B0LNnSyxcadUfKjQKHJMn6B6tXu\/JlwbGbe\/SdM8lT4fguLoEchFdoIAFMQS+GqZwYRpu\/sXUxH1B6U0oUrtw5YIBc6blZIrdQ\/pI6oN6L6f85dRxHUSLI7YmwFdlTnZwTw==","access_mid":"123456","timestamp":1694085373}',
            'result_info' => 1,
            'create_time' => date('Y-m-d H:i:s'),
            'type'        => '卡券'
        ];

        return $this->success($log);

    }
}
